<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/9
 * Time: 11:45
 */

namespace App\Http\Logics\MiniProgram;


use App\VipCard;
use App\VipCardRule;
use App\VipCardRuleValue;
use Illuminate\Support\Facades\DB;
use App\Http\Logics\BaseLogic;
class VipCardRuleLogic extends BaseLogic
{

    /**
     * 创建会员卡规则
     * @param $data 会员卡规则信息
     * @return mixed
     */
    public static function createVipCardRule($data)
    {
        try{
            // 创建事务，创建会员卡规则的同时更新会员卡与会员卡规则的关联表
            DB::transaction(function () use ($data) {
                $response = self::getPartnerIdByLoginSession($data['loginSession']);
                if ($response->code) {
                    return self::response(self::CONTENT_IS_EMPTY);
                }
                $partner_id = $response->data;
                $vip_card_rule = new VipCardRule();
                $vip_card_rule->name = $data['name'];
                $vip_card_rule->partner_id = $partner_id;
                if (!$vip_card_rule->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }

                $vip_card_rule_value_list_arr = explode(',', $data['vip_card_rule_value_list_str']);

                foreach ($vip_card_rule_value_list_arr as $item) {
                    $vip_card_rule_value = explode('_', $item);
                    $vip_card_id = $vip_card_rule_value[0];
                    $discount = $vip_card_rule_value[1];
                    $vip_card_rule_value = new VipCardRuleValue();
                    $vip_card_rule_value->vip_card_rule_id = $vip_card_rule->id;
                    $vip_card_rule_value->vip_card_id = $vip_card_id;
                    $vip_card_rule_value->discount = $discount;
                    if (!$vip_card_rule_value->save()) {
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                }
            });

            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 更新会员卡规则
     * @param $data 筛选条件
     * @return mixed
     */
    public static function updateVipCardRule($data)
    {
        try{
            // 创建事务，更新会员卡规则的同时更新会员卡与会员卡规则的关联表
            DB::transaction(function () use ($data) {
                // 获取到该会员卡规则
                $vip_card_rule = VipCardRule::find($data['vip_card_rule_id']);
                if (empty($vip_card_rule)) {
                    return self::response(self::CONTENT_IS_EMPTY);
                }

                // 更新会员卡规则信息
                $vip_card_rule->name = $data['name'];
                if (!$vip_card_rule->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }

                $vip_card_rule_value_list_arr = explode(',', $data['vip_card_rule_value_list_str']);

                // 更新会员卡规则和会员卡的中间表会员卡规则值表
                foreach ($vip_card_rule_value_list_arr as $item) {
                    $vip_card_rule_value = explode('_', $item);
                    $vip_card_id = $vip_card_rule_value[0];
                    $discount = $vip_card_rule_value[1];
                    $vip_card_rule_value = VipCardRuleValue::where('vip_card_id', $vip_card_id)->where('vip_card_rule_id', $vip_card_rule->id)->first();
                    $vip_card_rule_value->discount = $discount;
                    if (!$vip_card_rule_value->save()) {
                        return self::response(self::OBJECT_SAVE_FAIL);
                    }
                }
            });
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取会员卡规则列表
     * @param $data 筛选条件
     * @return mixed 返回结果
     */
    public static function getVipCardRuleList($data)
    {
        try{
            $response = self::getPartnerIdByLoginSession($data['loginSession']);
            if ($response->code){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $partner_id = $response->data;
            $vip_card_rule = VipCardRule::where('partner_id', $partner_id)->select('id', 'name')->get();
            if (empty($vip_card_rule)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $vip_card_rule);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function getVipCardRuleDetail($data)
    {
        $vip_card_rule = VipCardRule::find($data['vip_card_rule_id']);
        if (empty($vip_card_rule)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $vip_card_rule->vip_card = $vip_card_rule->vipCard()->select('vip_card.id', 'vip_card.name')->get();

        foreach ($vip_card_rule->vip_card as $value){
            $discount = VipCardRuleValue::where('vip_card_rule_id', $data['vip_card_rule_id'])->where('vip_card_id', $value->id)->first();
            $value->discount = $discount->discount;
        }
        return self::response(self::SUCCESS, $vip_card_rule);
    }

    /**
     * 删除会员卡规则
     * @param $data 筛选条件
     * @return mixed 返回结果
     */
    public static function deleteVipCardRule($data)
    {
        try {
            // 创建事务，删除会员卡规则的同时删除会员卡规则值
            DB::transaction(function () use ($data) {
                $vip_card_rule = VipCardRule::find($data['vip_card_rule_id']);
                if (empty($vip_card_rule)) {
                    return self::response(self::CONTENT_IS_EMPTY);
                }

                $vip_card_rule_value = VipCardRuleValue::where('vip_card_rule_id', $vip_card_rule->id)->get();
                foreach ($vip_card_rule_value as $item) {
                    if (!$item->delete()) {
                        return self::response(self::OBJECT_DELETE_FAILD);
                    }
                }

                if (!$vip_card_rule->delete()) {
                    return self::response(self::OBJECT_DELETE_FAILD);
                }
            });
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }
}